/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.account;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.account.SysRoleUser;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2012-12-10
 */
@Service("sysroleuserService")
public class SysRoleUserServiceImpl implements SysRoleUserService {
    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of SysRoleUser
     ****************************************/
    public void insert(SysRoleUser sysroleuser) throws Exception {
        iDataSet4iBatis.insert(sysroleuser);
    }

    /****************************************
     * Update a instace of SysRoleUser
     ****************************************/
    public void update(SysRoleUser sysroleuser) throws Exception {
        iDataSet4iBatis.update(sysroleuser);
    }

    /****************************************
     * Delete a instace of SysRoleUser
     ****************************************/
    public void delete(SysRoleUser sysroleuser) throws Exception {
        iDataSet4iBatis.delete(sysroleuser);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(List<?> sysRoleUsers) throws Exception {
        List<BaseModel> sysroleuserList = new ArrayList<BaseModel>();
        for (Object obj : sysRoleUsers) {
            SysRoleUser sysRoleUser = new SysRoleUser();
            BeanUtils.populate(sysRoleUser, (Map<?, ?>) obj);
            sysRoleUser.setStatmentId("SysRoleUser.insert");
            sysroleuserList.add(sysRoleUser);
        }
        iDataSet4iBatis.batchInsert(sysroleuserList);
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(SysRoleUser sysroleuser) throws Exception {
        String statementId = sysroleuser.getStatmentId();
        String mainId = sysroleuser.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> sysroleuserList = new ArrayList<BaseModel>();
        for (String id : ids) {
            SysRoleUser model = new SysRoleUser();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            sysroleuserList.add(model);
        }
        iDataSet4iBatis.batchDelete(sysroleuserList);
    }

    /****************************************
     * Query instaces of SysRoleUser
     ****************************************/
    public List<BaseModel> queryAsList(SysRoleUser sysroleuser) throws Exception {
        String queryListStatement = "";
        if (sysroleuser.isPaging()) {
            queryListStatement = sysroleuser.getStatmentId();
            sysroleuser.setStatmentId("SysRoleUser.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(sysroleuser);
            sysroleuser.setTotalCount(baseModel.getTotalCount());
            sysroleuser.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(sysroleuser);
    }

    /****************************************
     * Query a instaces of SysRoleUser
     ****************************************/
    public BaseModel queryAsObject(SysRoleUser sysroleuser) throws Exception {
        return iDataSet4iBatis.queryAsObject(sysroleuser);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
